﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Common;

public partial class website_admin_knowlegebase_projecteditor : PageBase
{
    string ProjectImages = ConfigurationManager.AppSettings["ProjectImages"].ToString();
    string ProjectTitleImages = ConfigurationManager.AppSettings["ProjectTitleImages"].ToString();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        /// 保存设置
        if (RequestBase.GetFormString("___EVENTTARGET") == "save")
        {
            Save();
        }

        if (!IsPostBack)
        { 
            if (RequestBase.GetQueryString("articleid") != "")
                Initialize();
        }
    }
     

    #region 数据初始化
    void Initialize()
    {
        Model.KbArticleBase _obj = BLL.KbArticleBase.GetObjById(RequestBase.GetQueryString("articleid"));
        ArticleObj = _obj;
        AttachmentsObj = BLL.AttachmentsBase.GetAttachments(RequestBase.GetQueryString("articleid"));
    }

    Model.KbArticleBase ArticleObj
    {
        get
        {
            Model.KbArticleBase _article = new Model.KbArticleBase();

            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("articleid")))
                _article.ArticleId = RequestBase.GetQueryString("articleid");

            _article.Title = txtTitle.Value.Trim();
            _article.ArticleNumber = int.Parse(string.IsNullOrEmpty(txtArticleNumber.Value.Trim()) && Utils.IsNumber(txtArticleNumber.Value) ? "0" : txtArticleNumber.Value.Trim());
            _article.CategoryId = "24A8226E-D7C8-4C5C-B15A-0E38D32AC230";
            _article.Resume = "";
            _article.Content = txtContent.Text;

            _article.ModifiedOn = DateTime.Now;
            _article.CreatedOn = DateTime.Now;
            _article.ModifiedBy = this.SystemUserId;
            _article.CreatedBy = this.SystemUserId;
            _article.TitlePic = txtUploadControl_ProjectTitleImages.Value.Trim().Replace("|", "");

            return _article;
        }
        set
        {
            if (value != null)
            {
                txtTitle.Value = value.Title.Trim();
                txtArticleNumber.Value = value.ArticleNumber.ToString();  
                txtContent.Text = value.Content;
                if (value.TitlePic != "")
                {
                    txtUploadControl_ProjectTitleImages.Value = "|" + value.TitlePic.Trim();
                    divProductImagesList_ProjectTitleImages.InnerHtml = "<div class=\"attachmentitems\">" + BLL.AttachmentsBase.GetIcoByExtName(value.TitlePic.Trim().Split('*')[2]) + "&nbsp;&nbsp;<a href='../../" + ProjectTitleImages + value.TitlePic.Trim().Split('*')[1] + "' title='Click to open the file' target='_blank'>" + value.TitlePic.Trim().Split('*')[0] + "</a>&nbsp;&nbsp;<a href='javascript:void(0);' filedata='" + value.TitlePic.Trim() + "' itemid='ProjectTitleImages' onclick='uploadinit($(this));'>[Delete]</a></div>";
                }
            }
        }
    } 

    Model.AttachmentsBase[] AttachmentsObj
    {
        get
        {
            if (txtUploadControl_ProjectImages.Value.Trim() != "")
            {
                string fileData = "#" + txtUploadControl_ProjectImages.Value;
                fileData = fileData.Replace("#|", "");
                string[] arrItems = fileData.Split('|');

                Model.AttachmentsBase[] fileItems = new Model.AttachmentsBase[arrItems.Length];
                int i = 0;
                foreach (string item in arrItems)
                {
                    fileItems[i] = new Model.AttachmentsBase("00000000-0000-0000-0000-000000000000", item.Split('*')[0], item.Split('*')[1], item.Split('*')[2], int.Parse(item.Split('*')[3]), decimal.Parse(item.Split('*')[4]), RequestBase.GetFormString("txtAttachmentFileNotes_" + item.Split('*')[1]), (i + 1).ToString());
                    i++;
                }
                return fileItems;
            }
            else
                return null;
        }
        set
        {
            if (value != null)
            {
                StringBuilder attachments = new StringBuilder();
                string _filedata = "";
                foreach (Model.AttachmentsBase _item in value)
                {
                    _filedata = _item.FileName + "*" + _item.FilePath + "*" + _item.ExtName + "*" + _item.FileType.ToString() + "*" + _item.FileSize.ToString();
                    attachments.Append("<div class=\"attachmentitems\">" + BLL.AttachmentsBase.GetIcoByExtName(_item.ExtName.Trim()) + "&nbsp;&nbsp;<a href='../../" + ProjectImages + _item.FilePath.Trim() + "' title='Click to open the file' target='_blank'>" + _item.FileName.Trim() + "</a><br />Notes:&nbsp;<input type=\"text\" style=\"width: 350px;\" name=\"txtAttachmentFileNotes_" + _item.FilePath.Trim() + "\" value=\"" + _item.Notes.Trim() + "\" />&nbsp;&nbsp;<a href='javascript:void(0);' filedata='" + _filedata + "' itemid='ProjectImages' onclick='uploadinit($(this));'>[Delete]</a></div>");
                    txtUploadControl_ProjectImages.Value += "|" + _filedata;
                }

                divProductImagesList_ProjectImages.InnerHtml = attachments.ToString();
            }
        }
    }
    #endregion

    #region 保存数据
    void Save()
    {
        string res = "";

        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {
            res = BLL.KbArticleBase.Add(ArticleObj, AttachmentsObj);
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            res = BLL.KbArticleBase.Update(ArticleObj, AttachmentsObj);
        }

        switch (res.Split('|')[0])
        {
            case "succeeded":
                JScript.AlertAndReturnValueToParent("Information saved successfully!", "window.opener.LoadingData(); window.close();");
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
    #endregion
}
